﻿using SmartSapce.ServiceLocation;
using SmartSpace.Core;
using SmartSpace.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SmartSpace.WebApp._console
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (_ctlEditAdminAccount.Text.ToLower() == "administrator" && _ctlEditAdminPassword.Text == "smartspace")
            {
                Logger.Debug("administrator login!");

                if (SsSite.Current == null)
                {
                    Response.Redirect("./wizzard/deploy-application-template.aspx");
                }
                else
                {
                    System.Web.Security.FormsAuthentication.SetAuthCookie("administrator", true);
                    Response.Redirect("./dashboard/index.aspx");
                }
            }
            else if (SsSite.Current != null)
            {
                Logger.Debug(string.Format("administrator login,SsSite.Current:{0}!", SsSite.Current.Id));
                ICoreService service = ServiceLocator.Get<ICoreService>();
                User user = service.GetUser(_ctlEditAdminAccount.Text.Trim());
                if (user == null)
                {
                    Logger.Debug(string.Format("administrator login,{0}不存在于系统中!", _ctlEditAdminAccount.Text));
                }
                //else if (user.Password == SmartSpace.Core.User.EncryptMD5(_ctlEditAdminPassword.Text) && user.Id == SsSite.Current.AdminUserId)
                else if (user.Id == SsSite.Current.AdminUserId)
                {
                    System.Web.Security.FormsAuthentication.SetAuthCookie(user.Account, true);
                    Response.Redirect("./dashboard/dashboard.aspx");
                }
                else
                {
                    Logger.Debug(string.Format("administrator login,{0}登录失败!", _ctlEditAdminAccount.Text));
                }
            }
        }
    }
}